﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.CustomerManager;
using PayrollHR.Web;
using System.Data;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.Utils;

namespace PayrollHR.Administration.Modules
{
    public partial class CaseProfilePersonal : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FillPaperDropDowns();
            }
        }
        private void FillPaperDropDowns()
        {
            ddlObjectID.Items.Clear();
            var courseCollection = this.CustomerService.GetObjectById(4);
            if (courseCollection.Count != 1)
            {
                courseCollection.Insert(0, new Objects { ObjectId = 0, ObjectName = "-None-" });
            }
            foreach (Objects feestype in courseCollection)
            {
                ListItem ddlcourseItem2 = new ListItem(feestype.ObjectName, feestype.ObjectId.ToString());
                ddlObjectID.Items.Add(ddlcourseItem2);
            }
        }
        public void BindCustomer(Customer item)
        {
            if (CommonHelper.IsNull(item))
                return;

            dpProDate.SelectedDate = CommonHelper.ToDate(item.ProDate);
            txtProCurator.Text = item.ProCurator;
            txtProFullName.Text = item.ProFullName;
            txtProSource.Text = item.ProSource;
            txtProSchool.Text = item.ProSchool;
            txtProProgram.Text = item.ProProgram;
            RdButtonListVisa.SelectedIndex = item.statusVisa;
            dpProDateOfAdmission.SelectedDate = CommonHelper.ToDate(item.ProDateOfAdmission);
            dpProscheduledInterviewDate.SelectedDate = CommonHelper.ToDate(item.ProscheduledInterviewDate);
            txtProOther.Text = item.ProOther;
            dpAddprofile.SelectText = item.Addprofile;
            dpAddprofileSchools.SelectText = item.AddprofileSchools;
            dpAddSeviceFreeSchools.SelectText = item.AddSeviceFreeSchools;
            dpAddhealthcare.SelectText = item.Addhealthcare;
            dpAddSeviceFree.SelectText = item.AddSeviceFree;
            dpAddI20offer.SelectText = item.AddI20offer;
            dpAddCheckI20offer.SelectText = item.AddCheckI20offer;
            dpRegistrationticket.SelectText = item.Registrationticket;
            dpRegistrationtaccommodation.SelectText = item.Registrationtaccommodation;
            dpRegistrationairport.SelectText = item.Registrationairport;
            dpTransferSchool.SelectText = item.TransferSchool;
            List<profile> customer = CustomerService.GetAllProfileByCustomerID(CustomerID, 4);
            BindGrid(customer);
        }
        protected void BindGrid(List<profile> dt)
        {
            ViewState["CaseProfilePersonal"] = dt;
            gvObject.DataSource = dt;
            gvObject.DataBind();
        }
        protected void gvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvObject.PageIndex = e.NewPageIndex;
            var customer = CustomerService.GetAllProfileByCustomerID(CustomerID, 4);
            BindGrid(customer);
        }
        private List<profile> getTableGridview()
        {
            List<profile> dtCurrentTable = new List<profile>();
            if (ViewState["CaseProfilePersonal"] != null)
            {
                dtCurrentTable = (List<profile>)ViewState["CaseProfilePersonal"];
            }
            return dtCurrentTable;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<profile> listProfile = getTableGridview();
            profile profile = new profile()
            {
                ProfileID = -1,
                ObjectID = CommonHelper.ToInteger(ddlObjectID.SelectedItem.Value),
                ObjectName = ddlObjectID.SelectedItem.Text,
                OriginalCopy = cb1OriginalCopy.Checked,
                duplicate = cbNotOriginalCopy.Checked,
                OnRequest = dpOnRequest.SelectText,
                DateSubmitted = dp1DateSubmitted.SelectText,
                Note = txtNote.Text,
                UpdateByID = PayrollContext.Current.Session.EmployeeId,
                CreateByID = PayrollContext.Current.Session.EmployeeId,
                CreatedOn = Convert.ToDateTime(DateTime.Today),
                UpdatedOn = Convert.ToDateTime(DateTime.Today)
            };
            listProfile.Add(profile);
            BindGrid(listProfile);
        }
        /// <summary>
        /// Saves the info.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns>Customer' Info</returns>
        public Customer GetCustomer(Customer customer)
        {
            customer.ProDate = dpProDate.SelectText;
            customer.ProCurator = txtProCurator.Text;
            customer.ProFullName = txtProFullName.Text;
            customer.ProSource = txtProSource.Text;
            customer.ProSchool = txtProSchool.Text;
            customer.ProProgram = txtProProgram.Text;
            customer.ProDateOfAdmission = dpProDateOfAdmission.SelectText;
            customer.ProscheduledInterviewDate = dpProscheduledInterviewDate.SelectText;
            customer.ProOther = txtProOther.Text;
            customer.statusVisa = RdButtonListVisa.SelectedIndex > -1 ? RdButtonListVisa.SelectedIndex : 4;
            customer.Addprofile = dpAddprofile.SelectText;
            customer.AddprofileSchools = dpAddprofileSchools.SelectText;
            customer.AddSeviceFreeSchools = dpAddSeviceFreeSchools.SelectText;
            customer.Addhealthcare = dpAddhealthcare.SelectText;
            customer.AddSeviceFree = dpAddSeviceFree.SelectText;
            customer.AddI20offer = dpAddI20offer.SelectText;
            customer.AddCheckI20offer = dpAddCheckI20offer.SelectText;
            customer.Registrationticket = dpRegistrationticket.SelectText;
            customer.Registrationtaccommodation = dpRegistrationtaccommodation.SelectText;
            customer.Registrationairport = dpRegistrationairport.SelectText;
            customer.TransferSchool = dpTransferSchool.SelectText;
            return customer;
        }
        public List<profile> GetProfile()
        {
            if (ViewState["CaseProfilePersonal"] != null)
            {
                List<profile> dt = (List<profile>)ViewState["CaseProfilePersonal"];
                return dt;
            }
            else
            {
                return new List<profile>();
            }
        }

        protected void gvObject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnDelete = e.Row.FindControl("lbDelete") as LinkButton;
                if (btnDelete != null)
                    btnDelete.CommandArgument = e.Row.RowIndex.ToString();
            }
        }
        protected void gvObject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                GridViewRow row = gvObject.Rows[index];
                HiddenField hfProfileID = row.FindControl("hfProfileID") as HiddenField;
                List<profile> listProfile = getTableGridview();
                if (listProfile != null)
                {
                    listProfile.RemoveAt(index);
                    if (CommonHelper.ToInteger(hfProfileID.Value) > 0)
                    {
                        List<int> List = new List<int>();
                        List.Add(CommonHelper.ToInteger(hfProfileID.Value));
                        this.CustomerService.DeleteProfile(List);
                    }
                    BindGrid(listProfile);
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }
        #region

        #endregion
    }
}